package com.sunshine.simplehospital.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RatingBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.button.MaterialButton
import com.sunshine.simplehospital.R
import kotlin.math.max

/**
 * Created by sunshine on 2020/4/29
 */
class RegisterAdapter : RecyclerView.Adapter<RegisterAdapter.RegisterViewHolder>(){

    private val count = max((Math.random() * 100).toInt(), 10)

    inner class RegisterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val star: RatingBar = itemView.findViewById(R.id.star)
        val starText: TextView = itemView.findViewById(R.id.star_text)
        val titleHospital: TextView = itemView.findViewById(R.id.title_hospital)
        val buttonRegister: MaterialButton = itemView.findViewById(R.id.button_register)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RegisterViewHolder {
        return RegisterViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_hospital, parent, false))
    }

    override fun getItemCount(): Int {
        return count
    }

    override fun onBindViewHolder(holder: RegisterViewHolder, position: Int) {
        val starScore = String.format("%.1f", Math.random() * 5).toFloat()
        holder.star.rating = starScore
        holder.starText.text = "${starScore}分"
        holder.titleHospital.text = "医院名称$position"
        holder.buttonRegister
    }


}